﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HKEA.CMS.Core.BLL.App;
using HKEA.CMS.Core.BLL.Tbl;
using HKEA.CMS.Core.BLL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using System.Transactions;
using System.Globalization;

namespace WebApplication
{
    public partial class M_Basicinformation : BasePage
    {
        protected string CustUID = string.Empty;
        protected static int UID = 0;
        tblCustomer entTblCust = new tblCustomer();
        tblCustomerBLL bllTblCust = new tblCustomerBLL();
        app_tblCustomer entAppCust = new app_tblCustomer();
        app_tblCustomerBLL bllAppCust = new app_tblCustomerBLL();
        tblMediaRoom_Related entMR_Related = new tblMediaRoom_Related();
        app_tblMediaRoom_Related entAppMR_Related = new app_tblMediaRoom_Related();
        List<tblMediaRoom_Related> listMRoom_Related = new List<tblMediaRoom_Related>();
        List<app_tblMediaRoom_Related> listAppMRoom_Related = new List<app_tblMediaRoom_Related>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                UID = GetEnt();
                if (UID > 0)
                {
                    CustUID = UID.ToString();
                    BindInfo();
                    BindMediaRoomRelated(UID);
                    BindLogoForCustomer(UID);
                }
                else {
                    usermeg.Visible = false;
                    SetNoOption();
                    lblMeg.Text = ResourceManage.GetResourceString("msg200002"); 
                }
            }
        }

        void BindInfo()
        {
            #region mtblCustType
            mtblCustType entType = new mtblCustType();
            mtblCustTypebBLL bllType = new mtblCustTypebBLL();
            #endregion

            #region             
            entTblCust.CustomerUID = UID;
            entTblCust.Deleted = Constant.IsDelete.no;
            entAppCust.CustomerUID = UID;
            entAppCust.Deleted = Constant.IsDelete.no;
            #endregion

            entTblCust = bllTblCust.GetEntity(entTblCust);
            entAppCust = bllAppCust.GetEntity(entAppCust);

            if (entTblCust != null && entAppCust == null)
            {
                string _CustomerType = bllType.GetEntity(entTblCust.CustTypeUID).CustType_Eng.ToString();
                CustomerSince.Text = GetDateTime(entTblCust.CustomerSince);
                CustomerType.Text = _CustomerType;
                CustomerID.Text = entTblCust.CustomerID;
                CustomerName.Text = entTblCust.Name_Eng;
                CustomerName_Chi.Text = entTblCust.Name_Chi;
                YearEstablished.Text =GetYear(entTblCust.EstDate);
                NoOfEmplogyee.Text = entTblCust.NoOfEmp.ToString();
                BR.Text = entTblCust.BR;
                txtHKID_PassportNo.Text = secretchar(entTblCust.HKID_PassportNo);
                hfHKID_PassprotNo.Value = entTblCust.HKID_PassportNo;
                MembershipSince.Text = GetYearAndMonth(entTblCust.EntryDate);
                ReferralBy.Text = entTblCust.ReferralBy;
                ReferralDate.Text = GetDateTime(entTblCust.ReferralDate);
                txtIntro_en.Text = entTblCust.Intro_Eng;
                txtIntro_hk.Text = entTblCust.Intro_Chi;
                OldIntro_en.Value = entTblCust.Intro_Eng;
                OldIntro_hk.Value = entTblCust.Intro_Chi;
            }
            else if (entAppCust != null)
            {
                string _CustomerType = bllType.GetEntity(entAppCust.CustTypeUID).CustType_Eng.ToString();
                CustomerSince.Text = GetDateTime(entAppCust.CustomerSince);
                CustomerType.Text = _CustomerType;
                CustomerID.Text = entAppCust.CustomerID;
                CustomerName.Text = entAppCust.Name_Eng;
                CustomerName_Chi.Text = entAppCust.Name_Chi;
                YearEstablished.Text = GetYear(entAppCust.EstDate);
                NoOfEmplogyee.Text = entAppCust.NoOfEmp.ToString();
                BR.Text = entAppCust.BR;
                txtHKID_PassportNo.Text = secretchar(entAppCust.HKID_PassportNo);
                hfHKID_PassprotNo.Value = entTblCust.HKID_PassportNo;
                MembershipSince.Text = GetYearAndMonth(entAppCust.EntryDate);
                ReferralBy.Text = entAppCust.ReferralBy;
                ReferralDate.Text = GetDateTime(entAppCust.ReferralDate);
                txtIntro_en.Text = entAppCust.Intro_Eng;
                txtIntro_hk.Text = entAppCust.Intro_Chi;
            }
            BindMediaRoom(UID);
        }
        /// <summary>
        /// 绑定上传图片区域的图片
        /// </summary>
        /// <param name="custUID"></param>
        private void BindMediaRoom(int custUID)
        {
            List<tblMediaRoom> listTbl = new List<tblMediaRoom>();
            tblMediaRoom entTbl = new tblMediaRoom();
            entTbl.CustomerUID = custUID; 
            entTbl.IsPublish = Constant.IsDelete.yes;
            entTbl.MediaType = Constant.MediaType.image;
            entTbl.Category = Constant.MediaRoomCategory.customer_logo;
            listTbl = tblMediaRoomBLL.GetInstance().GetListByENT(entTbl);
            List<app_tblMediaRoom> list = new List<app_tblMediaRoom>();
            app_tblMediaRoom entApp = new app_tblMediaRoom();
            entApp.CustomerUID = custUID;
            entApp.IsPublish = Constant.IsDelete.yes;
            entApp.MediaType = Constant.MediaType.image;
            entApp.Category = Constant.MediaRoomCategory.customer_logo;
            list = app_tblMediaRoomBLL.GetInstance().GetListByENT(entApp);

            for (int i = 0; i < listTbl.Count(); i++) {
                entApp = new app_tblMediaRoom();
                entApp.AppMediaRoomUID = listTbl[i].MediaRoomUID;
                entApp.Category = listTbl[i].Category;
                entApp.CustomerUID = listTbl[i].CustomerUID;
                entApp.MediaType = listTbl[i].MediaType;
                entApp.FilePath = listTbl[i].FilePath;
                entApp.FileName = listTbl[i].FileName;
                entApp.FileExt = listTbl[i].FileExt;
                list.Add(entApp);
            }

            rp_mediaroom.DataSource = list;
            rp_mediaroom.DataBind();

            for (int i = 0; i < list.Count(); i++)
            {
                mediaroomBound.Value += list[i].AppMediaRoomUID.ToString() + ",";
            }
        }
        /// <summary>
        /// 绑定MedioRoom数据
        /// </summary>
        /// <param name="customerUID"></param>
        /// <returns></returns>
        private List<Ext_MediaRoom> BindMediaRoomRelated(int customerUID)
        {
            List<Ext_MediaRoom> list = new List<Ext_MediaRoom>();
            List<Ext_MediaRoom> listTmp = new List<Ext_MediaRoom>();
            Ext_MediaRoom ent = new Ext_MediaRoom();
            ent.RelatedUID = customerUID;
            ent.Is_Publish = Constant.IsPublish.yes;
            ent.MediaType = Constant.MediaType.image;
            ent.Category = Constant.MediaRoomCategory.customer_logo;
            list = tblMediaRoomRelatedBLL.GetInstance().GetListWithMediaRoomByENT(ent);
            listTmp = app_tblMediaRoom_RelatedBLL.GetInstance().GetListWithMediaRoomByENT(ent);

                for (int i = 0; i < listTmp.Count(); i++)
                {
                    ent = new Ext_MediaRoom();
                    ent.MediaRoomRelatedUID = listTmp[i].MediaRoomRelatedUID;
                    ent.Category = listTmp[i].Category;
                    ent.RelatedUID = listTmp[i].RelatedUID;
                    ent.Deleted = listTmp[i].Deleted;
                    ent.MediaRoomUID = listTmp[i].MediaRoomUID;
                    ent.MediaType = listTmp[i].MediaType;
                    ent.FilePath = listTmp[i].FilePath;
                    ent.FileName = listTmp[i].FileName;
                    ent.FileAlt_Eng = listTmp[i].FileAlt_Eng;
                    ent.FileAlt_Chi = listTmp[i].FileAlt_Eng;
                    ent.FileLink_Eng = listTmp[i].FileLink_Eng;
                    ent.FileLink_Chi = listTmp[i].FileLink_Chi;
                    ent.Des_Eng = listTmp[i].Des_Eng;
                    ent.Des_Chi = listTmp[i].Des_Chi;
                    ent.IsPublish = listTmp[i].IsPublish;//关联的字段
                    ent.LastModifyBy = listTmp[i].LastModifyBy;
                    ent.CreatedAt = listTmp[i].CreatedAt;
                    ent.UpdatedAt = listTmp[i].UpdatedAt;
                    list.Add(ent);
                }
            
            return list;
        }
        /// <summary>
        /// 绑定选中图片区域的图片
        /// </summary>
        /// <param name="customerUID"></param>
        private void BindLogoForCustomer(int customerUID)
        {
            if (customerUID > 0)
            {
                List<Ext_MediaRoom> list = BindMediaRoomRelated(customerUID);
                Ext_MediaRoom ent = new Ext_MediaRoom();
                ent.RelatedUID = customerUID;
                ent.Is_Publish = Constant.IsPublish.yes;
                ent.MediaType = Constant.MediaType.image;
                ent.Category = Constant.MediaRoomCategory.customer_logo;

                #region
                entAppMR_Related.RelatedUID = customerUID;
                entAppMR_Related.IsPublish = Constant.IsPublish.yes;
                entAppMR_Related.Category = Constant.MediaRoomCategory.customer_logo;
                List<app_tblMediaRoom_Related> appList = app_tblMediaRoom_RelatedBLL.GetInstance().GetListByENT(entAppMR_Related);
                #endregion

                foreach (app_tblMediaRoom_Related C in appList)
                {
                    if (C.MediaRoomUID != null & C.MediaRoomUID > 0)
                    {
                        var result = list.Where(a => a.MediaRoomUID == C.MediaRoomUID).Select(a => a).ToList();
                        foreach (Ext_MediaRoom I in result)
                        {
                            list.Remove(I);
                        }
                    }
                }

                ent.Deleted = Constant.IsDelete.no;
                List<Ext_MediaRoom> listTmp = app_tblMediaRoom_RelatedBLL.GetInstance().GetListWithMediaRoomByENT(ent, true);

                for (int i = 0; i < listTmp.Count(); i++)
                {
                    ent = new Ext_MediaRoom();
                    ent.MediaRoomRelatedUID = listTmp[i].MediaRoomRelatedUID;
                    ent.Category = listTmp[i].Category;
                    ent.RelatedUID = listTmp[i].RelatedUID;
                    ent.Deleted = listTmp[i].Deleted;
                    ent.MediaRoomUID = listTmp[i].MediaRoomUID;
                    ent.MediaType = listTmp[i].MediaType;
                    ent.FilePath = listTmp[i].FilePath;
                    ent.FileName = listTmp[i].FileName;
                    ent.FileAlt_Eng = listTmp[i].FileAlt_Eng;
                    ent.FileAlt_Chi = listTmp[i].FileAlt_Eng;
                    ent.FileLink_Eng = listTmp[i].FileLink_Eng;
                    ent.FileLink_Chi = listTmp[i].FileLink_Chi;
                    ent.Des_Eng = listTmp[i].Des_Eng;
                    ent.Des_Chi = listTmp[i].Des_Chi;
                    ent.IsPublish = listTmp[i].IsPublish;//关联的字段
                    ent.LastModifyBy = listTmp[i].LastModifyBy;
                    ent.CreatedAt = listTmp[i].CreatedAt;
                    ent.UpdatedAt = listTmp[i].UpdatedAt;
                    list.Add(ent);
                }


                rpLogo.DataSource = list;
                rpLogo.DataBind();

                hfnotbind_media.Value = "";
                //辅助变量
                for (int i = 0; i < list.Count(); i++)
                {
                    hfnotbind_media.Value += list[i].MediaRoomUID.ToString() + ",";
                }
                Oldhfnotbind_media.Value = hfnotbind_media.Value;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            UID = GetEnt();
            string msg = string.Empty;
            if (UID > 0)
            {
                if (CheckEnt() == true)
                {
                    List<app_tblMediaRoom_Related> addlist = new List<app_tblMediaRoom_Related>();

                    #region 获取删除的数据的数组
                    List<int> deltelist = new List<int>();
                    string[] _mediaroom = mediaroomBound.Value.Split(',');
                    string[] _appMediaRoomUID = hfnotbind_media.Value.Split(',');
                    for (int i = 0; i < _mediaroom.Length - 1; i++)
                    {
                        deltelist.Add(Convert.ToInt32(_mediaroom[i]));
                    }

                    for (int j = 0; j < _appMediaRoomUID.Length - 1; j++)
                    {
                        deltelist.Remove(Convert.ToInt32(_appMediaRoomUID[j]));
                    }
                    #endregion

                    #region
                    entMR_Related.RelatedUID = UID; entMR_Related.Deleted = -1;
                    entAppMR_Related.RelatedUID = UID; entAppMR_Related.Deleted = -1;
                    listMRoom_Related = tblMediaRoomRelatedBLL.GetInstance().GetListByENT(entMR_Related);
                    listAppMRoom_Related = app_tblMediaRoom_RelatedBLL.GetInstance().GetListByENT(entAppMR_Related);
                    #endregion

                    for (int i = 0; i < _appMediaRoomUID.Length - 1; i++)
                    {
                        entAppMR_Related = new app_tblMediaRoom_Related();
                        int _mediaRoomUID = Convert.ToInt32(_appMediaRoomUID[i]);
                        var resuleTbl = listMRoom_Related.Where(a => a.RelatedUID == UID && a.MediaRoomUID == _mediaRoomUID).ToList();
                        var resuleApp = listAppMRoom_Related.Where(a => a.RelatedUID == UID && a.AppMediaRoomUID == _mediaRoomUID).ToList();

                        #region
                        if (resuleApp.Count() > 0)
                        {
                            foreach (app_tblMediaRoom_Related C in resuleApp)
                            {
                                entAppMR_Related.RelatedUID = UID;
                                entAppMR_Related.MediaRoomRelatedUID = C.MediaRoomRelatedUID;
                                entAppMR_Related.MediaRoomUID = C.MediaRoomUID;
                                entAppMR_Related.AppMediaRoomUID = C.AppMediaRoomUID;
                                entAppMR_Related.Deleted = Constant.IsDelete.no;
                                entAppMR_Related.Category = Constant.MediaRoomCategory.customer_logo;
                                entAppMR_Related.IsPublish = Constant.IsPublish.yes;
                            }
                        }
                        else if (resuleApp.Count() == 0 && resuleTbl.Count() > 0)
                        {
                            foreach (tblMediaRoom_Related C in resuleTbl)
                            {
                                entAppMR_Related.RelatedUID = UID;
                                entAppMR_Related.MediaRoomRelatedUID = C.MediaRoomRelatedUID;
                                entAppMR_Related.MediaRoomUID = C.MediaRoomUID;
                                //entAppMR_Related.AppMediaRoomUID = 0;
                                entAppMR_Related.Deleted = Constant.IsDelete.no;
                                entAppMR_Related.Category = Constant.MediaRoomCategory.customer_logo;
                                entAppMR_Related.IsPublish = Constant.IsPublish.yes;
                            }
                        }
                        else //新增数据 2->2)
                        {
                            var tem_tbl_Muid = tblMediaRoomBLL.GetInstance().GetEntity(_mediaRoomUID);

                            entAppMR_Related.RelatedUID = UID;
                            entAppMR_Related.MediaRoomRelatedUID = 0;
                            if (tem_tbl_Muid != null)
                            {
                                entAppMR_Related.MediaRoomUID = _mediaRoomUID; ;
                                //entAppMR_Related.AppMediaRoomUID = 0;
                            }
                            else
                            {
                                //entAppMR_Related.MediaRoomUID = 0;
                                entAppMR_Related.AppMediaRoomUID = _mediaRoomUID;
                            }
                            entAppMR_Related.Category = Constant.MediaRoomCategory.customer_logo;
                            entAppMR_Related.Deleted = Constant.IsDelete.no;
                            entAppMR_Related.IsPublish = Constant.IsPublish.yes;
                        }
                        addlist.Add(entAppMR_Related);
                        #endregion
                    }

                    TransactionOptions sptions = new TransactionOptions();
                    sptions.Timeout = new TimeSpan(0, 5, 0);
                    sptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
                    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, sptions))
                    {                        
                        UpdateInfo();
                        AddItem(addlist);
                        DeleteItem(deltelist);
                        WriteToLog();
                        try
                        {

                            scope.Complete();
                            msg = ResourceManage.GetResourceString("msg200004");
                        }
                        catch (Exception ex)
                        {
                            msg = ResourceManage.GetResourceString("msg200003");
                        }
                    }

                }
                else
                {
                    msg = SetNoOption();
                    msg = ResourceManage.GetResourceString("msg200014");
                }
            }
            else
            {
                LbtnSave.Enabled = false;
                msg = ResourceManage.GetResourceString("msg400004");
            }

            lblMeg.Text = msg;
            BindLogoForCustomer(UID);
            //Response.Redirect("~/M_Basicinformation.aspx");
        }

        protected int UpdateInfo() {
            int tal = 0;
            #region
            entTblCust = new tblCustomer();
            
            entTblCust.CustomerUID = UID;
            entTblCust.Deleted = Constant.IsDelete.no;
            entAppCust.CustomerUID = UID;
            entAppCust.Deleted = Constant.IsDelete.no;
            #endregion

            entTblCust = bllTblCust.GetEntity(entTblCust);
            entAppCust = bllAppCust.GetEntity(entAppCust);
            if (entAppCust != null)
            {
                entAppCust.Intro_Eng = hfTxtIntro_en.Value;
                entAppCust.Intro_Chi = hfTxtIntro_hk.Value;
                tal = bllAppCust.UpdateENT(entAppCust);
            }
            else if (entAppCust == null && entTblCust != null)
            {
                entAppCust = new app_tblCustomer();
                entAppCust.CustomerSince = entTblCust.CustomerSince;
                entAppCust.CustomerUID = entTblCust.CustomerUID;
                entAppCust.CustomerID = entTblCust.CustomerID;
                entAppCust.Name_Eng = entTblCust.Name_Eng;
                entAppCust.Name_Chi = entTblCust.Name_Chi;
                entAppCust.CustTypeUID = entTblCust.CustTypeUID;
                entAppCust.EstDate = entTblCust.EstDate;
                entAppCust.NoOfEmp = entTblCust.NoOfEmp;
                entAppCust.BR = entTblCust.BR;
                entAppCust.HKID_PassportNo = entTblCust.HKID_PassportNo;
                entAppCust.EntryDate = entTblCust.EntryDate;
                entAppCust.ReferralBy = entTblCust.ReferralBy;
                entAppCust.ReferralDate = entTblCust.ReferralDate;
                entAppCust.Intro_Eng = txtIntro_en.Text;
                entAppCust.Intro_Chi = txtIntro_hk.Text;
                tal = bllAppCust.InsertENT(entAppCust);
            }
           return tal;
        }

        protected void AddItem(List<app_tblMediaRoom_Related> item) {
            #region
            List<app_tblMediaRoom_Related> tmpApplist = new List<app_tblMediaRoom_Related>();
            entAppMR_Related = new app_tblMediaRoom_Related();
            entAppMR_Related.RelatedUID = UID;
            entAppMR_Related.Category = Constant.MediaRoomCategory.customer_logo;
            entAppMR_Related.Deleted = -1;
            tmpApplist = app_tblMediaRoom_RelatedBLL.GetInstance().GetListByENT(entAppMR_Related);
            List<tblMediaRoom_Related> tmpTbllist = new List<tblMediaRoom_Related>();
            entMR_Related = new tblMediaRoom_Related();
            entMR_Related.RelatedUID = UID;
            entMR_Related.Category = Constant.MediaRoomCategory.customer_logo;
            entMR_Related.Deleted = 0;
            tblMediaRoomRelatedBLL bll = new tblMediaRoomRelatedBLL();
            //tmpTbllist = tblMediaRoomRelatedBLL.GetInstance().GetListByENT(entMR_Related);
            tmpTbllist = bll.GetListByENT(entMR_Related);
            #endregion

            for (int i = 0; i < item.Count(); i++)
            {
                int App_tal = -1;

                if (Convert.ToInt32(item[i].AppMediaRoomUID) > 0 & Convert.ToInt32(item[i].MediaRoomUID) == 0)
                     App_tal = tmpApplist.Where(a => a.AppMediaRoomUID == item[i].AppMediaRoomUID).Count();
                else if (Convert.ToInt32(item[i].MediaRoomUID) > 0 & Convert.ToInt32(item[i].AppMediaRoomUID) == 0)
                    App_tal = tmpApplist.Where(a => a.MediaRoomUID == item[i].MediaRoomUID).Count();
                else if (Convert.ToInt32(item[i].MediaRoomUID) > 0 & Convert.ToInt32(item[i].AppMediaRoomUID) > 0)
                    App_tal = tmpApplist.Where(a => a.AppMediaRoomUID == item[i].AppMediaRoomUID & a.MediaRoomUID == item[i].MediaRoomUID).Count();

                int tbl_tal = tmpTbllist.Where(a => a.MediaRoomUID == item[i].MediaRoomUID).Count();

                if (App_tal <= 0 && tbl_tal <= 0)
                {
                    AddItem(item[i] as app_tblMediaRoom_Related,UID);
                }
                else if (App_tal >= 1)
                {
                    List<app_tblMediaRoom_Related> val=new List<app_tblMediaRoom_Related>();
                    if (Convert.ToInt32(item[i].AppMediaRoomUID) > 0 & Convert.ToInt32(item[i].MediaRoomUID) == 0)
                        val = tmpApplist.Where(a => a.AppMediaRoomUID == item[i].AppMediaRoomUID).ToList();
                    else if (Convert.ToInt32(item[i].MediaRoomUID) > 0 & Convert.ToInt32(item[i].AppMediaRoomUID) == 0)
                        val = tmpApplist.Where(a => a.MediaRoomUID == item[i].MediaRoomUID).ToList();
                    else if (Convert.ToInt32(item[i].MediaRoomUID) > 0 & Convert.ToInt32(item[i].AppMediaRoomUID) > 0)
                        val = tmpApplist.Where(a => a.AppMediaRoomUID == item[i].AppMediaRoomUID && a.MediaRoomUID == item[i].MediaRoomUID).ToList();

                    // App_中有对应的数据,表示当前这条数据并不需要更改,所以把原来在App_中临时的数据取消更改,需要删除App_对应的数据
                    int tamp = val.Where(a => a.Deleted == Constant.IsDelete.yes).Count();
                    if (tamp >= 1)
                    {
                        foreach (app_tblMediaRoom_Related C in val)
                        {
                            DeleteItem(C, UID);
                        }
                    }
                }
            }
        }

        protected void DeleteItem(List<int> item) {
            #region 初始化App 和Tbl的数据
            entMR_Related = new tblMediaRoom_Related();
            entAppMR_Related = new app_tblMediaRoom_Related();
            entMR_Related.RelatedUID = UID;
            entAppMR_Related.RelatedUID = UID;
            entMR_Related.Category = Constant.MediaRoomCategory.customer_logo;
            entAppMR_Related.Category = Constant.MediaRoomCategory.customer_logo;
            List<app_tblMediaRoom_Related> ApptempList = new List<app_tblMediaRoom_Related>();
            app_tblMediaRoom_RelatedBLL bll = new app_tblMediaRoom_RelatedBLL();
            ApptempList=bll.GetListByENT(entAppMR_Related);
            List<tblMediaRoom_Related> tmpTbllist = new List<tblMediaRoom_Related>();
            entMR_Related.RelatedUID = UID;
            entMR_Related.Deleted = 0;
            tmpTbllist = tblMediaRoomRelatedBLL.GetInstance().GetListByENT(entMR_Related);
            #endregion

            for (int i = 0; i < item.Count(); i++)
            {
                #region 初始化需要删除的对象实体->app_cust_busNatureENT
                entAppMR_Related = new app_tblMediaRoom_Related();
                entAppMR_Related.RelatedUID = UID;
                #endregion


                var App_tal = ApptempList.Where(a => a.AppMediaRoomUID == item[i] | a.MediaRoomUID == item[i]).ToList();


                var Tbl_tal = tmpTbllist.Where(a => a.MediaRoomUID == item[i]).ToList();
                if (App_tal.Count() >= 1 && Tbl_tal.Count() == 0)
                {
                    foreach (app_tblMediaRoom_Related C in App_tal)
                    {
                        DeleteItem(C, UID);
                    }
                }
                else if (App_tal.Count() == 0 && Tbl_tal.Count() == 1)
                {
                    var items = tmpTbllist.Where(a => a.RelatedUID == UID && a.MediaRoomUID ==item[i]).ToList();
                    foreach (tblMediaRoom_Related C in items)
                    {
                        //entAppMR_Related.AppMediaRoomUID = null;                        
                        entAppMR_Related.MediaRoomRelatedUID = C.MediaRoomRelatedUID;
                        entAppMR_Related.RelatedUID = C.RelatedUID;
                        entAppMR_Related.MediaRoomUID = C.MediaRoomUID;
                        entAppMR_Related.Deleted = Constant.IsDelete.yes;
                        entAppMR_Related.Category = C.Category;
                        entAppMR_Related.IsPublish = Constant.IsPublish.no;
                        AddItem(entAppMR_Related, UID);
                    }
                }
            }
        }

        /// <summary>
        /// 将tbl_info更新到App_Tbl_info
        /// </summary>
        /// <param name="ent"></param>
        void UpDateTbltoApp(app_tblMediaRoom_Related ent)
        {
            entAppMR_Related = new app_tblMediaRoom_Related();
            entAppMR_Related.AppMediaRoomRelatedUID = ent.AppMediaRoomRelatedUID;
            entAppMR_Related.RelatedUID = ent.RelatedUID;
            entAppMR_Related.AppMediaRoomUID = ent.AppMediaRoomUID;
            if (ent.MediaRoomUID>0)
                entAppMR_Related.MediaRoomUID = ent.MediaRoomUID;
            if (ent.MediaRoomRelatedUID>0)
                entAppMR_Related.MediaRoomRelatedUID = ent.MediaRoomRelatedUID;
            entAppMR_Related.Deleted = ent.Deleted;
            entAppMR_Related.IsPublish =ent.IsPublish ;
            app_tblMediaRoom_RelatedBLL.GetInstance().UpdateENT(entAppMR_Related);
        }

        /// <summary>
        /// member修改了记录,临时记录将会添加到App_表中
        /// </summary>
        /// <param name="item"></param>
        /// <param name="Cuid"></param>
        void AddItem(app_tblMediaRoom_Related item,int UID)
        {
            entAppMR_Related = new app_tblMediaRoom_Related();
            entAppMR_Related.RelatedUID = item.RelatedUID;
            entAppMR_Related.AppMediaRoomUID = item.AppMediaRoomUID;
            if (item.MediaRoomUID>0)
                entAppMR_Related.MediaRoomUID = item.MediaRoomUID;
            if (item.MediaRoomRelatedUID>0)
                entAppMR_Related.MediaRoomRelatedUID = item.MediaRoomRelatedUID;
            entAppMR_Related.Category = item.Category;
            entAppMR_Related.Deleted = item.Deleted;
            entAppMR_Related.IsPublish = item.IsPublish;
            app_tblMediaRoom_RelatedBLL.GetInstance().InsertENT(entAppMR_Related);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <param name="Cuid"></param>
        void UpdateItem(app_tblMediaRoom_Related item)
        {
            entAppMR_Related = new app_tblMediaRoom_Related();
            entAppMR_Related.RelatedUID = item.RelatedUID;
            entAppMR_Related.AppMediaRoomUID = item.AppMediaRoomUID;
            if (item.MediaRoomUID>0)
                entAppMR_Related.MediaRoomUID = item.MediaRoomUID;
            if (item.MediaRoomRelatedUID>0)
                entAppMR_Related.MediaRoomRelatedUID = item.MediaRoomRelatedUID;
            entAppMR_Related.Deleted = item.Deleted;
            entAppMR_Related.IsPublish = item.IsPublish;
            app_tblMediaRoom_RelatedBLL.GetInstance().UpdateENT(entAppMR_Related);
        }

        /// <summary>
        /// 删除临时表的记录
        /// </summary>
        /// <param name="item"></param>
        /// <param name="Cuid"></param>
        void DeleteItem(app_tblMediaRoom_Related item, int Cuid)
        {
            entAppMR_Related.RelatedUID = Cuid;
            entAppMR_Related.AppMediaRoomRelatedUID = item.AppMediaRoomRelatedUID;
            entAppMR_Related.MediaRoomUID = item.MediaRoomUID;
            app_tblMediaRoom_RelatedBLL.GetInstance().DeleteENT(entAppMR_Related);
        }


        #region 网页公共方法 1.通过Session获取当前的CustomerUID 2.获取时间的两组方法,以及身份证星号显示
        /// <summary>
        /// 
        /// </summary>
        private string SetNoOption()
        {
            string msg=string.Empty;
            LbtnSave.Enabled = false;
            msg= ResourceManage.GetResourceString("msg200001");
            return msg;
        }

        /// <summary>
        /// 检查实体是否为空
        /// </summary>
        /// <returns></returns>
        bool CheckEnt()
        {
            bool ret = true;
            tblCustomer customerENT = new tblCustomer();
            tblCustomerBLL customerBLL = new tblCustomerBLL();
            customerENT.Deleted = 0;
            customerENT.CustomerUID = GetEnt();
            try
            {
                var tempENT = customerBLL.GetEntity(customerENT);
                if (tempENT == null)
                {
                    ret = false;
                }
            }
            catch (Exception ex)
            {
                ret = false;
            }
            return ret;
        }

        string GetYear(DateTime? date)
        {
            string result = "";
            if (date == null)
            {
                return result = "";
            }
            else
            {
                result = Convert.ToDateTime(date).ToString("yyyy", new CultureInfo("en-US"));
            }
            return result;
        }
        string GetYearAndMonth(DateTime? date)
        {
            string result = "";
            if (date == null)
            {
                return result = "";
            }
            else
            {
                result = Convert.ToDateTime(date).ToString("MMM yyyy", new CultureInfo("en-US"));
            }
            return result;
        }
        string GetDateTime(DateTime? date)
        {
            string result = "";
            if (date == null)
            {
                return result = "";
            }
            else
            {
                result = ((DateTime)date).ToString("MMM yyyy", new CultureInfo("en-US")); 
            }
            return result.ToString();
        }
        /// <summary>
        /// 身份证为*号显示
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        string secretchar(string value)
        {
            string ret = string.Empty;
            if (!string.IsNullOrEmpty(value))
            {
                ret = value;
                if (value.Trim() != "" && value.Trim().Length > 4)
                {
                    var length = value.Trim().Length;
                    var star = "";
                    for (int i = 0; i < length - 4; i++)
                    {
                        star += "*";
                    }
                    ret = value.Trim().Substring(0, 3) + star + value.Trim().Substring(length - 1);
                }
            }
            return ret;
        }
        #endregion

        protected void WriteToLog()
        {
            string ChangeLoge = string.Empty;
            string LoginName = tblCust_WebLoginBLL.GetInstance().GetENT(GetMemberLoginUID()).LoginName.ToString();
            if (Convert.ToInt32(tempVal.Value) == 1)
            {
                ChangeLoge += " Logo / Trademark have been Change";
            }

            if (hfTxtIntro_en.Value != OldIntro_en.Value | hfTxtIntro_hk.Value != OldIntro_hk.Value) {
                tblHistory entlog = new tblHistory();
                Literal CompanyName = usermeg.FindControl("ltlCompanyName_eng") as Literal;
                entlog.HistoryCatUID = 2;
                entlog.HistoryTypeUID = 36;
                entlog.CustomerUID = GetEnt();
                entlog.Cust_MemberUID = 0;
                entlog.ContactPersonUID = 0;
                entlog.History_Detail = LoginName + " {" + CompanyName.Text + "}{Basicinformation} have change the Introduction" + ChangeLoge;
                entlog.LastModifyBy = LoginName;
                int tal = tblHistoryBLL.GetInstance().InsertENT(entlog);
            }
        }
        protected void LbtnQuit_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/M_Basicinformation.aspx");
        }

    }
}